﻿package com.pb.news4.dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

import com.pb.news4.dao.BaseDao;
import com.pb.news4.dao.News4Dao;
import com.pb.news4.entity.News4;

public class News4DaoImpl extends BaseDao implements News4Dao {
	// 查询新闻信息
	public List<News4> getNewsList(){
		List<News4> news4List = new ArrayList<News4>();
		//（3）获得Statement对象，执行SQL语句
		String sql="select n.*,c.name as categoryName from news_detail n,"
				+ "news_category c where n.categoryId = c.id";
		Object[] params={};
		if(getConnection()){
			ResultSet rs=this.executeSQL(sql, params);
			//（4）处理执行结果(ResultSet)，
			try {
				while(rs.next()){
					int id=rs.getInt("id");
					String title=rs.getString("title");
					String summary=rs.getString("summary");
					String content=rs.getString("content");
					String author=rs.getString("author");
					Timestamp createDate=rs.getTimestamp("createDate");
					String categoryName = rs.getString("categoryName");
					//System.out.println(id + "\t" + title + "\t" + summary + "\t"+ content + "\t" + author + "\t" + createDate);
					News4 news4 = new News4();
					news4.setId(id);
					news4.setTitle(title);
					news4.setSummary(summary);
					news4.setContent(content);
					news4.setAuthor(author);
					news4.setCreateDate(createDate);
					news4.setCategoryName(categoryName);
					news4List.add(news4);
				}
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		//释放资源
		this.closeResource();
		return news4List;
	}

	// 增加新闻信息
	public boolean add(News4 news4) {
		boolean flag = false;
		String sql="insert into news_detail(id,categoryId,title,author,summary,content,createDate) values(?,?,?,?,?,?,?)";
		Object[] params={news4.getId(),news4.getCategoryId(),news4.getTitle(),news4.getAuthor(),news4.getSummary(),news4.getContent(), new java.sql.Timestamp(news4.getCreateDate().getTime())};
		if(getConnection()){
			int i=this.executeUpdate(sql, params);
			//（4）处理执行结果
			if(i>0){
				System.out.println("插入新闻成功！");
				flag = true;
			}
		}
		//释放资源
		this.closeResource();
		return flag;
	}
	
	// 删除新闻信息
	public boolean delete(News4 news4) {
		boolean flag = false;
		String sql = "delete from  news_detail where id=?";
		Object[] params={news4.getId()};
		if(getConnection()){
			int i=this.executeUpdate(sql, params);
			//（4）处理执行结果
			if(i>0){
				System.out.println("删除新闻成功！");
				flag = true;
			}
		}
		//释放资源
		this.closeResource();
		return flag;
	}

	// 修改新闻标题信息
	public boolean update(News4 news4) {
		boolean flag = false;
		String sql = "update  news_detail set title=? where id=?";
		Object[] params={news4.getTitle(),news4.getId()};
		if(getConnection()){
			int i=this.executeUpdate(sql, params);
			//（4）处理执行结果
			if(i>0){
				System.out.println("修改新闻成功！");
				flag = true;
			}
		}
		//释放资源
		this.closeResource();
		return flag;
	}
	
	/*@Override
	public boolean deleteNewsCategory(NewsCategory newsCategory) {
		boolean flag = false;
		// TODO Auto-generated method stub
		String deleteNewsDetailsql = "delete from news_detail where categoryId=?";
		String deleteNewsCategorysql = "delete from news_category where id=?";
		Object[] params={newsCategory.getId()};
		// 先删字表news_detail，再删主表news_category
		if(getConnection()){
			int i = this.executeUpdate(deleteNewsDetailsql, params);
			if(i != -1){
				int j = this.executeUpdate(deleteNewsCategorysql, params);
				if(j > 0){
					System.out.println("删除新闻类别成功！");
					flag = true;
				}
			}
		}
		//释放资源
		this.closeResource();
		return flag;
	}*/
	
	@Override
	public int getNews4Count(News4 news4) {
		// TODO Auto-generated method stub
		int count = 0;
		String sql = "select count(1) count from news_detail where categoryId = ?";
		Object [] params = {news4.getCategoryId()};
		if(getConnection()){
			ResultSet rs = this.executeSQL(sql, params);
			try {
				if(rs.next()){
					count = rs.getInt("count");
				}
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}finally {
				this.closeResource();
			}
		}
		return count;
	}
	
	//测试
	public static void main(String[] args) {
		News4DaoImpl news4Dao=new News4DaoImpl();
		/*News4 news4 = new News4();
		news4.setId(34);
		news4.setTitle("快女十强选秀");*/
		/*news4.setCategoryId(13);
		news4.setTitle("快女选秀");
		news4.setSummary("快女比赛正进入白热化");
		news4.setContent("她、她、她，谁是你心中的快女冠军？");
		news4.setAuthor("admin");
		news4.setCreateDate(new Date());*/
//		news4Dao.add(news4);
//		news4Dao.update(news4);
//		news4Dao.delete(news4);
		/*NewsCategory newsCategory = new NewsCategory();
		newsCategory.setId(5);
		news4Dao.deleteNewsCategory(newsCategory);*/
		List<News4> news4List = new ArrayList<News4>();
		news4List = news4Dao.getNewsList();
		for(News4 _news4 : news4List){
			System.out.println("id:" + _news4.getId() + "\ttitle:" + _news4.getTitle() 
			+ "\tsummary:" + _news4.getSummary() + "\tauthor:" 
			+ _news4.getAuthor() + "\tcreateDate:" + _news4.getCreateDate() + "\tcategoryName:" + _news4.getCategoryName());
		}
	}
}
